Tutorial: Getting started with multi-module workspaces
Create the workspace
go work init ./hello
The go work init command tells go to create a go.work file for a workspace containing the modules in the ./hello directory.
The go command produces a go.work file that looks like this:
// go.work
go 1.18
use ./hello
The go.work file has similar syntax to go.mod.
The go directive tells Go which version of Go the file should be interpreted with. It’s similar to the go directive in the go.mod file.
The use directive tells Go that the module in the hello directory should be main modules when doing a build.
So in any subdirectory of workspace the module will be active.
Create a sub library
# current directory /hello
cd ../
mkdir test
cd test
go mod init example.com/test
create a file test.go
package test
func Hello() string {
return "Hello World"
}
In hello work space
go work use ../test
package main
import (
"fmt"
"example/test"
)
func main() {
fmt.Println(test.Hello())
}